1   /************************************************************
2   *                     Copyright                            *
3   * Portions of this software are Copyright (c) 1993 - 2002, *
4   * Chad Z. Hower (Kudzu) and the Indy Pit Crew              *
5   *  - http://www.nevrona.com/Indy/                          *
6   ************************************************************/
7   package org.indy.util;
8   
9   import java.io.File;
10  import java.io.FileInputStream;
11  import java.io.FileOutputStream;
12  import java.io.IOException;
13  
14  import java.text.DecimalFormat;
15  import java.text.ParseException;
16  import java.text.SimpleDateFormat;
17  
18  import java.util.ArrayList;
19  import java.util.Arrays;
20  import java.util.Calendar;
21  import java.util.Collection;
22  import java.util.Date;
23  import java.util.Properties;
24  import java.util.ResourceBundle;
25  import java.util.StringTokenizer;
26  
27  import org.indy.io.IndyIOException;
28  
29  
30  /***
31   *  IdGlobal 27/11/01: OTG - Created from Delphi IdGlobal unit
32   *
33   *@author    owen
34   */
35  public final class IndyUtilities {
36    private static ResourceBundle resourceStrings = ResourceBundle.getBundle(
37                                                        "org.indy.IdResourceStrings");
38  
39    //private static ResourceBundle indyProperties    ;
40    private static Properties properties = new Properties();
41  
42    static {
43      properties.setProperty("indy.default.timeout", "120000");
44      properties.setProperty("indy.default.maxline", String.valueOf(16 * 1024));
45    }
46  
47    private final static String m_productName = "IndyJ";
48    private final static String indyVersion = "9.00";
49  
50    /***
51     *  Description of the Field
52     */
53    protected final static String m_datePattern = "dd MMM yyyy HH:mm:ss";
54  
55    /***
56     * DOCUMENT ME!
57     *
58     * @param key DOCUMENT ME!
59     *
60     * @return DOCUMENT ME!
61     */
62    public static String getIndyProperty(String key) {
63      return properties.getProperty(key);
64    }
65  
66    /***
67     * DOCUMENT ME!
68     *
69     * @return DOCUMENT ME!
70     */
71    public static int getDefaultTimeout() {
72      return Integer.parseInt(properties.getProperty("indy.default.timeout"));
73    }
74  
75    /***
76     * DOCUMENT ME!
77     *
78     * @return DOCUMENT ME!
79     */
80    public static int getDefaultMaxLineLength() {
81      return Integer.parseInt(properties.getProperty("indy.default.maxline"));
82    }
83  
84    /***
85     *  Parses a string based on a pattern and retrieves its numneric equivalent
86     *  from the default Calendar instance
87     *
88     *@param  val                    Description of the Parameter
89     *@param  pattern                Description of the Parameter
90     *@param  field                  Description of the Parameter
91     *@return                        The calendarField value
92     *@throws  DateParseException  Description of the Exception
93     */
94    protected static int getCalendarField(String val, String pattern, int field)
95                                   throws DateParseException {
96      SimpleDateFormat sdf = new SimpleDateFormat(pattern);
97  
98      try {
99        Calendar cal = Calendar.getInstance();
100 
101       cal.setTime(sdf.parse(val));
102 
103       return cal.get(field);
104     }
105      catch (ParseException pe) {
106       throw new DateParseException(pe);
107     }
108   }
109 
110   /***
111    *  Gets the resourceString attribute of the IdGlobal class
112    *
113    *@param  key  Description of the Parameter
114    *@return      The resourceString value
115    */
116   public static String getResourceString(String key) {
117     return resourceStrings.getString(key);
118   }
119 
120   /***
121    *  Gets the productName attribute of the IdGlobal class
122    *
123    *@return    The productName value
124    */
125   public static String getProductName() {
126     return m_productName;
127   }
128 
129   /***
130    *  Gets the indyVersion attribute of the IdGlobal class
131    *
132    *@return    The indyVersion value
133    */
134   public static String getIndyVersion() {
135     return indyVersion;
136   }
137 
138   /***
139    *  Gets the iP attribute of the IdGlobal class
140    *
141    *@param  ip  Description of the Parameter
142    *@return     The iP value
143    */
144   public static boolean isIP(String ip) {
145     StringTokenizer tokens = new StringTokenizer(ip, ".");
146 
147     int tokenCount = 0;
148 
149     while (tokens.hasMoreTokens()) {
150       if (tokenCount++ >= 4) {
151         return false;
152       }
153 
154       try {
155         Byte.parseByte(tokens.nextToken());
156       }
157        catch (NumberFormatException nfe) {
158         return false;
159       }
160     }
161 
162     return true;
163   }
164 
165   /***
166    *  Gets the mIMETypeFromFile attribute of the IdGlobal class
167    *
168    *@param  filename  Description of the Parameter
169    *@return           The mIMETypeFromFile value
170    */
171   public static String getMIMETypeFromFile(String filename) {
172     /*** @todo Implement */
173     throw new UnsupportedOperationException();
174   }
175 
176   /***
177    *  Gets the currentThread attribute of the IdGlobal class
178    *
179    *@param  t  Description of the Parameter
180    *@return    The currentThread value
181    */
182   public static boolean isCurrentThread(Thread t) {
183     return Thread.currentThread().equals(t);
184   }
185 
186   /***
187    *  Description of the Method
188    *
189    *@param  path  Description of the Parameter
190    *@return       Description of the Return Value
191    */
192   public static String includeTrailingBackSlash(String path) {
193     String delim = System.getProperty("file.separator");
194 
195     if (!path.endsWith(delim)) {
196       return path + delim;
197     }
198     else {
199       return path;
200     }
201   }
202 
203   /***
204    *
205    */
206   public static void buildMIMETypeMap() {
207     /*** @todo Implement this */
208     throw new UnsupportedOperationException("Not done yet! Sorry");
209   }
210 
211   /***
212    *  Description of the Method
213    *
214    *@param  col  Description of the Parameter
215    *@param  src  Description of the Parameter
216    */
217   public static void commaSeperatedToCollection(Collection col, String src) {
218     StringTokenizer tokens = new StringTokenizer(src, ",");
219 
220     while (tokens.hasMoreTokens()) {
221       col.add(tokens.nextToken());
222     }
223   }
224 
225   /***
226    *  Description of the Method
227    *
228    *@param  src             Description of the Parameter
229    *@param  dest            Description of the Parameter
230    *@return                 Description of the Return Value
231    *@throws  IdIOException  Description of the Exception
232    */
233   public static boolean copyFileTo(String src, String dest)
234                             throws IndyIOException {
235     boolean result = true;
236 
237     FileInputStream input = null;
238     FileOutputStream output = null;
239 
240     try {
241       input = new FileInputStream(src);
242       output = new FileOutputStream(dest);
243 
244       int read = -1;
245 
246       while ((read = input.read()) != -1) {
247         output.write(read);
248       }
249     }
250      catch (IOException ioe) {
251       throw new IndyIOException(ioe);
252     }
253      finally {
254       if (input != null) {
255         try {
256           input.close();
257         }
258          catch (IOException ioe) {
259         }
260       }
261 
262       if (output != null) {
263         try {
264           output.flush();
265           output.close();
266         }
267          catch (IOException ioe) {
268           throw new IndyIOException(ioe);
269         }
270       }
271     }
272 
273     return result;
274   }
275 
276   /***
277    *  Description of the Method
278    *
279    *@param  date    Description of the Parameter
280    *@param  subGMT  Description of the Parameter
281    *@return         Description of the Return Value
282    */
283   public static String dateToGMTOffsetString(Date date, boolean subGMT) {
284     Calendar cal = Calendar.getInstance();
285 
286     cal.setTime(date);
287 
288     long offset = (cal.get(Calendar.ZONE_OFFSET) + 
289                   cal.get(Calendar.DST_OFFSET)) / 36000;
290 
291     DecimalFormat df = new DecimalFormat();
292 
293     df.setParseIntegerOnly(true);
294     df.setMaximumIntegerDigits(4);
295     df.setMinimumIntegerDigits(4);
296     df.setPositivePrefix("+");
297     df.setNegativePrefix("-");
298     df.setGroupingSize(5);
299 
300     if (subGMT) {
301       return "GMT" + df.format(offset);
302     }
303     else {
304       return df.format(offset);
305     }
306   }
307 
308   /***
309    *  Description of the Method
310    *
311    *@param  d  Description of the Parameter
312    *@return    Description of the Return Value
313    */
314   public static String dateToUnixString(Date d) {
315     SimpleDateFormat sdf = new SimpleDateFormat(m_datePattern);
316 
317     String result = sdf.format(d);
318     String offset;
319 
320     if (!(offset = dateToGMTOffsetString(d, false)).equals("+0000")) {
321       result += offset;
322     }
323 
324     return result;
325   }
326 
327   /***
328    *  Description of the Method
329    *
330    *@param  input   Description of the Parameter
331    *@param  delim   Description of the Parameter
332    *@param  delete  Description of the Parameter
333    *@return         Description of the Return Value
334    */
335   public static String fetch(String input, String delim, boolean delete) {
336     int pos = input.indexOf(delim);
337 
338     String result = input.substring(0, pos - 1);
339 
340     if (delete) {
341       input = input.substring(pos + 1);
342     }
343 
344     return result;
345   }
346 
347   /***
348    *  Description of the Method
349    *
350    *@param  input   Description of the Parameter
351    *@param  delete  Description of the Parameter
352    *@return         Description of the Return Value
353    */
354   public static String fetch(String input, boolean delete) {
355     return fetch(input, " ", delete);
356   }
357 
358   /***
359    *  Description of the Method
360    *
361    *@param  input  Description of the Parameter
362    *@param  delim  Description of the Parameter
363    *@return        Description of the Return Value
364    */
365   public static String fetch(String input, String delim) {
366     return fetch(input, delim, true);
367   }
368 
369   /***
370    *  Description of the Method
371    *
372    *@param  input  Description of the Parameter
373    *@return        Description of the Return Value
374    */
375   public static String fetch(String input) {
376     return fetch(input, " ", true);
377   }
378 
379   /***
380    *  Description of the Method
381    *
382    *@param  filename  Description of the Parameter
383    *@return           Description of the Return Value
384    */
385   public static long fileSizeByName(String filename) {
386     return new File(filename).length();
387   }
388 
389   /***
390    *  Description of the Method
391    *
392    *@return    Description of the Return Value
393    */
394   public static Collection indyPorts() {
395     /***
396      *@todo    use a hash map?
397      */
398     return new ArrayList();
399   }
400 
401   /***
402    *  Description of the Method
403    *
404    *@param  URI       Description of the Parameter
405    *@param  protocol  Description of the Parameter
406    *@param  host      Description of the Parameter
407    *@param  path      Description of the Parameter
408    *@param  document  Description of the Parameter
409    *@param  port      Description of the Parameter
410    */
411   public static void parseURI(String URI, String protocol, String host, 
412                               String path, String document, String port) {
413     /*** @todo Is this worth doing if we have a URI class? */
414   }
415 
416   /***
417    *  Description of the Method
418    *
419    *@param  searchString   Description of the Parameter
420    *@param  contents       Description of the Parameter
421    *@param  caseSensitive  Description of the Parameter
422    *@return                Description of the Return Value
423    */
424   public static int posInStringArray(String searchString, String[] contents, 
425                                      boolean caseSensitive) {
426     if (caseSensitive) {
427       return Arrays.binarySearch(contents, searchString);
428     }
429 
430     int res = -1;
431 
432     for (int i = 0; i < contents.length; i++) {
433       if (searchString.equalsIgnoreCase(contents[i])) {
434         res = i;
435       }
436     }
437 
438     return res;
439   }
440 
441   /***
442    *  Description of the Method
443    *
444    *@param  searchString  Description of the Parameter
445    *@param  contents      Description of the Parameter
446    *@return               Description of the Return Value
447    */
448   public static int posInStringArray(String searchString, String[] contents) {
449     return posInStringArray(searchString, contents, true);
450   }
451 
452   /***
453    *  Description of the Method
454    *
455    *@param  val    Description of the Parameter
456    *@param  shift  Description of the Parameter
457    *@return        Description of the Return Value
458    */
459   public static int ROL(int val, byte shift) {
460     //Not sure about this
461     return (val << shift) | (val >>> (32 - shift));
462   }
463 
464   /***
465    *  Description of the Method
466    *
467    *@param  val    Description of the Parameter
468    *@param  shift  Description of the Parameter
469    *@return        Description of the Return Value
470    */
471   public static int ROR(int val, byte shift) {
472     //Not sure about this, either
473     return (val >>> shift) | (val << (32 + shift));
474   }
475 
476   /***
477    *  Description of the Method
478    *
479    *@param  time  Description of the Parameter
480    */
481   public static void sleep(long time) {
482     try {
483       Thread.sleep(time);
484     }
485      catch (InterruptedException ex) {
486     }
487   }
488 
489   /***
490    *  Description of the Method
491    *
492    *@param  day                    Description of the Parameter
493    *@return                        Description of the Return Value
494    *@throws  DateParseException  Description of the Exception
495    */
496   public static int strToDay(String day) throws DateParseException {
497     return getCalendarField(day, "E", Calendar.DAY_OF_WEEK);
498   }
499 
500   /***
501    *  Description of the Method
502    *
503    *@param  month                  Description of the Parameter
504    *@return                        Description of the Return Value
505    *@throws  DateParseException  Description of the Exception
506    */
507   public static int strToMonth(String month) throws DateParseException {
508     return getCalendarField(month, "MMM", Calendar.MONTH);
509   }
510 
511   /***
512    *  Description of the Method
513    *
514    *@param  s  Description of the Parameter
515    *@return    Description of the Return Value
516    */
517   public static String upCaseFirst(String s) {
518     return s.substring(0, 1).toUpperCase() + s.substring(1).toLowerCase();
519   }
520 
521   /***
522    *  Description of the Method
523    *
524    *@param  date                   Description of the Parameter
525    *@return                        Description of the Return Value
526    *@throws  DateParseException  Description of the Exception
527    */
528   public static Date unixDateStrToDate(String date) throws DateParseException {
529     try {
530       //Remove day of week
531       int pos;
532 
533       if ((pos = date.indexOf(",")) > 0) {
534         date = date.substring(pos + 1);
535       }
536 
537       //Remove trailing timezome, e.g. (EST), (CDT)
538       if ((pos = date.indexOf("(")) > 0) {
539         date = date.substring(0, pos - 1);
540       }
541 
542       //see if it has a numerical GMT offset
543       SimpleDateFormat sdf = null;
544 
545       if ((date.indexOf("+") > 0) || (date.indexOf("-") > 0)) {
546         sdf = new SimpleDateFormat(m_datePattern + " zzzz");
547       }
548       else {
549         sdf = new SimpleDateFormat(m_datePattern);
550       }
551 
552       return sdf.parse(date);
553     }
554      catch (ParseException pe) {
555       throw new DateParseException(pe);
556     }
557   }
558 }
This page was automatically generated by Maven